package br.com.financeiro.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.com.financeiro.controllers.PapelMBean;
import br.com.financeiro.dominio.Papel;

//@FacesConverter(value = "papelConverter")
@FacesConverter(forClass = Papel.class, value = "papelConverter")
public class PapelConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value)
			throws ConverterException {
		if (value != null && value.equals("")) {
			PapelMBean papelMBean = new PapelMBean();
			Papel papel = papelMBean.buscar(Integer.parseInt(value));
			return papel;
		} else {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object object)
			throws ConverterException {
		
		if(object == null) {
			return "";
		}
		
		try {
			if(!object.equals(new Papel())) {
				return String.valueOf(object);
			}
		}
		catch (ConverterException e){
			e.printStackTrace();
		}
		return null;
	}

}
